package com.jingdianjichi.subject.application.handler.subject;//package com.jingdianjichi.subject.application.handler.subject;

import com.google.common.base.Preconditions;

import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.service.SubjectInfoService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMultipleService;
import com.jingdinajichi.subject.common.enmus.IsDeleteCodeEnum;
import com.jingdinajichi.subject.common.enmus.SubjectInfoTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zwy
 * @version 1.0
 * @description: MultipleTypeHandler
 * @date 2025/7/13 17:48
 */
@Component
@Slf4j
public class MultipleTypeHandler implements SubjectTypeHandler{
    @Resource
    private SubjectMultipleService subjectMultipleService;
    @Resource
    private SubjectInfoService subjectInfoService;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectLabelService subjectLabelService;

    /**
     * 枚举题目类型的识别
     *
     * @return
     */
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    /**
     *多选题目的插入
     *
     * @param subjectInfoBO
     */
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        if(log.isInfoEnabled()){
            log.info("MultipleTypeHandler.add.subjectInfoBO:{}",subjectInfoBO);
        }
        try{
            Preconditions.checkNotNull(subjectInfoBO.getSubjectOptionList(),"题目选项不能为空");
            List<SubjectOptionBO> subjectOptionBOList = subjectInfoBO.getSubjectOptionList();
//        将subjectInfoBO转换成subjectMultiple
        List<SubjectMultiple> subjectMultiples = new ArrayList<>();
        for(SubjectOptionBO subjectOptionBO:subjectOptionBOList){
            SubjectMultiple subjectMultiple = new SubjectMultiple();
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setOptionType(subjectOptionBO.getOptionType());
            subjectMultiple.setOptionContent(subjectOptionBO.getOptionContent());
            subjectMultiple.setIsCorrect(subjectOptionBO.getIsCorrect());
            subjectMultiple.setIsDeleted(IsDeleteCodeEnum.NOT_DELETE_CODE_ENUM.getCode());
            subjectMultiples.add(subjectMultiple);
        }
        //批量插入
        subjectMultipleService.insertBatch(subjectMultiples);
        }catch (Exception e){
            log.error("MultipleTypeHandler.add.error:{}",e);}

    }

    @Override
    public SubjectInfoBO query(SubjectInfoBO subjectInfoBO) {
        //通过题目id查询单选题目的标签ids,将标签ids用来查询对应的标签名称


        List<SubjectMultiple> subjectMultiples = subjectMultipleService.queryBySubjectId(subjectInfoBO.getId());
        List<SubjectOptionBO> subjectOptionBOList = subjectMultiples.stream().map(subjectMultiple -> {
            SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
            subjectOptionBO.setOptionType(subjectMultiple.getOptionType());
            subjectOptionBO.setOptionContent(subjectMultiple.getOptionContent());
            subjectOptionBO.setIsCorrect(subjectMultiple.getIsCorrect());
            return subjectOptionBO;
        }).collect(Collectors.toList());
        subjectInfoBO.setSubjectOptionList(subjectOptionBOList);

        return subjectInfoBO;
    }
}
